

/**
 * @author Administrator
 *
 */
import java.awt.*;
import java.awt.event.*;
import java.util.LinkedList;

public class LineTool extends Tool {
   
  protected Point startingMousePosition;
  protected Point currentMousePosition;
  protected Color saveColor;

  public LineTool(DrawingCanvas canvas, LinkedList<ShapeObject> ShapeObjects, MDUser mdUser) {
	  super(canvas, ShapeObjects, mdUser);
	  }
  
  protected void drawLineSegment(Point p1, Point p2) {
    canvas.getImageBufferGraphics().drawLine(p1.x,p1.y,p2.x, p2.y);
    int x0 = Math.min(p1.x, p2.x);
    int y0 = Math.min(p1.y, p2.y);
    int dx = Math.abs(p2.x - p1.x)+ 1;
    int dy = Math.abs(p2.y - p1.y) + 1;
    canvas.repaint(x0, y0, dx, dy);
  }

 public void mousePressed(MouseEvent e)  {
   startingMousePosition = e.getPoint();
   currentMousePosition = startingMousePosition;
   Graphics iBGraphics = canvas.getImageBufferGraphics();
   saveColor = iBGraphics.getColor( );
   iBGraphics.setXORMode(Color.lightGray);
   iBGraphics.setColor(Color.white);
   drawLineSegment(startingMousePosition, startingMousePosition);
 }

 public void mouseDragged(MouseEvent e)  {
   drawLineSegment(startingMousePosition,
				currentMousePosition);
   Point newMousePosition = e.getPoint();
   drawLineSegment(startingMousePosition,newMousePosition);
   currentMousePosition = newMousePosition;
 }


   public void mouseReleased(MouseEvent e) { 
     Graphics iBGraphics = canvas.getImageBufferGraphics();
     drawLineSegment(startingMousePosition,currentMousePosition);
		ShapeObjects.add(new LineObject(startingMousePosition, currentMousePosition, saveColor));	    
		iBGraphics.setPaintMode();
     iBGraphics.setColor(saveColor);
     paintObjects(0);
  }
   public void keyPressed(KeyEvent e) {}
}
